package com.yufei.entity.goods;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class GoodsPropScoreComputorimpl implements GoodsScoreComputor {
	private static Log mLog = LogFactory.getLog(GoodsPropScoreComputorimpl.class);
	private GoodsPropScoreGetter goodsPropScoreGetter=new DefaultGoodsPropScoreGetter();

	private static Map<String,String> tagPropsMap=new HashMap<>();
	private void initTagPropsMap() throws IOException{
		if(tagPropsMap.size()==0){
			InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("notebook_tag_props");
			BufferedReader br=new BufferedReader(new InputStreamReader(in));
			String line=null;
			do {
				line = br.readLine();
				if (line != null && !"".equals(line.trim())) {
					tagPropsMap.put(line.split(":")[0], line.split(":")[1]);
				}
			} while (line != null);
		}
	}


	@Override
	public int computerScore(Goods goods,String tag) {
		// TODO Auto-generated method stub
		int score=0;
		Map<String,Object> props=goods.getProperties();
		String[] propKeys=tagPropsMap.get(tag).split(",");
		for(String propKey:propKeys){
			score+=goodsPropScoreGetter.getScoreByPropKey(propKey, String.valueOf(props.get(propKey)));
		}
		
		return score;
	}

}
